package com.hotel.service.impl;

import com.hotel.component.IocContainer;
import com.hotel.dao.LodgingInfoDao;
import com.hotel.entity.LodgingInfo;
import com.hotel.service.LodgingInfoService;
import com.hotel.utils.JdbcUtils;

import java.sql.Connection;
import java.util.UUID;

/**
 * @Author Str
 * @Date 2021/11/4 10:07
 * @Description :
 */
public class LodgingInfoServiceImpl implements LodgingInfoService {
    private LodgingInfoDao lodgingInfoDao = (LodgingInfoDao) IocContainer.getBean("LodgingInfoDao");

    @Override
    public void save(LodgingInfo lodgingInfo) throws Exception {
        //{ps}这里要同时处理两件事情 [新增 / 更新]
        //{1}获取数据库连接.
        Connection conn = JdbcUtils.getConnection();
        //{2}开启事务功能 [自动提交关闭]
        conn.setAutoCommit(false);
        try {
            //{3}如果 userID 为空 ----> 新增用户
            if (lodgingInfo.getId() == null) {
                //{b}设置id
                //{b}创建一个 uuid [A87E....]
                String uuid = UUID.randomUUID().toString();
                //(原)"A87E-C5E6-.."  ----> (新)"A87EC5E6.."
                uuid = uuid.replace("-", "");
                //{b}设置给 user
                lodgingInfo.setId(uuid);
                //{c}新增用户
                lodgingInfoDao.addLodgingInfo(lodgingInfo);
            } else {    //不为空 ---> 更新用户
                lodgingInfoDao.updateLodgingInfo(lodgingInfo);
            }
            //{4}提交事务
            conn.commit();
        } finally {
            conn.close();
        }
    }
}
